home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / oostrng.exe / OOSTRING.H < prev    next >
C/C++ Source or Header  |  1992-02-13  |  3KB  |  108 lines

  1. // OOSTRING.H
  2. //
  3. // Class library for strings
  4.  
  5. ///////////////////////////////////////////////////////////////////////////
  6.  
  7. #ifndef __cplusplus
  8. #error Must use C++ for the type string.
  9. #endif
  10.  
  11. #ifndef __OOSTRING_H
  12. #define __OOSTRING_H
  13.  
  14. #ifndef NULL
  15. #if defined(__TINY__) || defined(__SMALL__) || defined(__MEDIUM__)
  16. #define NULL    0
  17. #else
  18. #define NULL    0L
  19. #endif
  20. #endif
  21.  
  22. #if   defined(__SMALL__) || defined(__MEDIUM__)
  23. #define _CLASSTYPE  near
  24. #elif defined(__COMPACT__) || defined(__LARGE__)
  25. #define _CLASSTYPE  far
  26. #else
  27. #define _CLASSTYPE  huge
  28. #endif
  29.  
  30.  
  31. ///////////////////////////////////////////////////////////////////////////
  32.  
  33. #include ".\stacks\object.h"
  34. #include <iostream.h>
  35. #include <iomanip.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <ctype.h>
  39.  
  40. ///////////////////////////////////////////////////////////////////////////
  41.  
  42. class _CLASSTYPE string : public object
  43. {
  44.   protected:
  45.  
  46.   char *string_ptr;                    // character array
  47.   int size;                            // allocated space
  48.   int length;                          // length of string
  49.  
  50.   public:
  51.  
  52.   string() : string_ptr(NULL), size(0), length(0) {};  // void constructor
  53.   string(char *str);                   // char array
  54.   string(char ch);                     // character
  55.   string(const string &src);           // copy constructor
  56.   ~string()                            // destructor
  57.   {
  58.     delete string_ptr;                 // release storage space
  59.   }
  60.  
  61.   string& operator=(const string &src);// assignment operator
  62.   friend string operator+(const string &str1, const string &str2);
  63.   string operator+=(const string &str);
  64.   string operator+();                  // unary operator +
  65.   string operator-();                  // unary operator -
  66.   string& operator++();                // convert to uppercase
  67.   string& operator--();                // convert to lowercase
  68.   char& operator[](const int index);   // allow access to chars in string.  Can be used as an lvalue
  69.   friend int operator>(const string &str1, const string &str2)
  70.   {
  71.     return (strcmp(str1.string_ptr, str2.string_ptr) > 0);
  72.   }  // end of operator >
  73.  
  74.   friend int operator<(const string &str1, const string &str2)
  75.   {
  76.     return (strcmp(str1.string_ptr, str2.string_ptr) < 0);
  77.   }  // end of operator <
  78.  
  79.   friend int operator==(const string &str1, const string &str2)
  80.   {
  81.     return (strcmp(str1.string_ptr, str2.string_ptr) == 0);
  82.   }  // end of operator ==
  83.  
  84.   friend int operator!=(const string &str1, const string &str2)
  85.   {
  86.     return !(strcmp(str1.string_ptr, str2.string_ptr) == 0);
  87.   }  // end of operator !=
  88.  
  89.   friend int operator>=(const string &str1, const string &str2)
  90.   {
  91.     return ((str1 > str2) || (str1 == str2));
  92.   }  // end of operator >=
  93.  
  94.   friend int operator<=(const string &str1, const string &str2)
  95.   {
  96.     return ((str1 < str2) || (str1 == str2));
  97.   }  // end of operator <=
  98.  
  99.   friend ostream& operator<<(ostream &stream, const string &str);
  100.   friend ostream& operator<<(ostream &stream, const string *str);
  101.   friend istream& operator>>(istream& stream, string& str);
  102.  
  103.   int setsize(const int newsize);
  104.   int len() { return (length);}
  105.   void getline(istream& stream, const int maxlen = 80);
  106. };  // end of class string
  107.  
  108. #endif